<?php

namespace app\souther\controller;
use app\common\model\Notice as NoticeModel;
use think\Db;

class Notice extends Base
{
	function __construct()
	{
		parent::_initialize();
	}

	/**
     * [index 列表]
     * @return [type] [description]
     * @author [南風]
     */

	public function index()
    {
        $model = new NoticeModel();

        if(request()->isAjax())
        {
            $data = input('param.');
            $map = [];

            if(is_array($data) && !empty($data['key'])){
                $map['title'] = ['like',"%" . $data['key'] . "%"];
            }
            $this->getPageAndSize($data);

            try{
                $count =$model->getNoticeCountByCondition($map);
                $allpage = intval(ceil($count / $this->size));//总页面
                $lists = $model->getNoticeByCondition($map, $this->from, $this->size);//列表
            }catch (\Exception $e)
            {
                writelog($e->getMessage(),-1);
                exception($e->getMessage());
            }

            writelog('查看公告列表');

            if(empty($lists))
                return '';

            return $this->fetch('ajax_list',[
                'count' => $count,//总记录数
                'allpage' => $allpage,
                'lists' => $lists,
            ]);
        }

        return $this->fetch('',['app'=>'notice']);
    }

    /**
     * [add 添加]
     * @return [type] [description]
     * @author [南風]
     */
    public function add()
    {
        if(request()->isAjax()){

            $model = new NoticeModel();

            $param = input('post.');
            
            try{
                $id = $model->addData($param);
            }catch(\Exception $e){
                writelog($e->getMessage(),-1);
                return admin_json(-1, $e->getMessage());
            }
            writelog('添加公告--'.$id);

            return admin_json(1,'成功','',url('/notice'));
        }

        return $this->fetch();
    }

    /**
     * [edit 编辑]
     * @return [type] [description]
     * @author [南風]
     */
    public function edit()
    {
        $model = new NoticeModel();

        if(request()->isAjax()){

            $param = input('post.');
            
            try{
                $id = $model->editData($param);
            }catch(\Exception $e){
                writelog($e->getMessage(),-1);
                return admin_json(-1, $e->getMessage());
            }
            writelog('编辑公告--'.$id);

            return admin_json(1,'成功','',url('/notice'));
        }

        return $this->fetch('edit',[
            'edit' => $model->findData(input('param.id'))
        ]);
    }

    /**
     * [del  删除]
     * @author [南風]
     * @return array
     */
    public function del()
    {
        $model = new NoticeModel();

        $id = input('param.id');

        $model->deleteTrueData($id);

        writelog('删除公告--'.$id);

        return admin_json(1,'成功');
    }

    /**
     * [status 状态]
     * @author [南風]
     * @return array
     */
    public function status()
    {
        $model = new NoticeModel();
        $data=input('param.');
        $res = $model->changeStatus(['id' => $data['id']],$data['status']);

        if($res)
        {
            if($data['status'] == 0)
            {
                writelog('禁用公告状态--'.$data['id']);
                $statusurl = url('status',['id'=>$data['id'],'status'=>1]);
                return admin_json(2,'已禁止',['statusurl'=>$statusurl]);
            }
            else
            {
                writelog('启用共告状态--'.$data['id']);
                $statusurl = url('status',['id'=>$data['id'],'status'=>0]);
                return admin_json(1,'已启用',['statusurl'=>$statusurl]);
            }
        }
        return admin_json(-1,'操作失败');
    }
}